package net.tksl.crm.util;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

public class WebUtil {

	static Logger logger = Logger.getLogger(WebUtil.class);
	
	public static void crearAtributoSesion(String clave,Object objeto){
		ActionContext contexto = ActionContext.getContext();
		Map<String,Object> mapaSesion = contexto.getSession();
		mapaSesion.put(clave,objeto);
	}
	
	public static Object leerAtributoSesion(String clave){
		ActionContext contexto = ActionContext.getContext();
		Map<String,Object> mapaSesion = contexto.getSession();
		return mapaSesion.get(clave);
	}
	
	public static void eliminarAtributoSesion(String clave){
		ActionContext contexto = ActionContext.getContext();
		Map<String,Object> mapaSesion=contexto.getSession();
		mapaSesion.remove(clave);
	}
	
	public static String obtenerRutaRaiz(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String rutaRaiz = "";
		rutaRaiz = request.getScheme()+"://"+request.getServerName();
		Integer puerto = request.getServerPort();
		if(puerto!=80){
			rutaRaiz = rutaRaiz + ":"+puerto;
		}
		rutaRaiz = rutaRaiz + request.getContextPath();
		return rutaRaiz;
	}
	
	public static String convertirStringISO_UTF8(String texto){
		String cadena = "";
		if(texto!=null && !texto.equals("")){
    		byte bytes[] = null;
			try {
				bytes = texto.getBytes("iso-8859-1");
				cadena = new String(bytes,"UTF-8");
			} catch (UnsupportedEncodingException e) {
				LoggerUtil.error(logger,e.getMessage());
			}
    	}
		
		return cadena;
	}
}
